
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  query.condition;

import java.util.Hashtable;
import java.util.Vector;

import logger.PoolLogger;

import utility.CapitalChar;
import dbmanager.DBManager;
import dbmanager.GetIdName;


public class Report_master implements Condition 
{
	Vector<Object> Conditionvector=new Vector<Object>();
	String QUERY;
	String object;
	String buffer[];
	Hashtable<Object, Object> Conditionmap = new Hashtable<Object, Object>();
	Hashtable<Object, Object> transaction_detailsmap = new Hashtable<Object, Object>();
	int Querycount=1; 
	GetIdName gid;
	String id;
	
	
	public Report_master() 
	{
	
	
	}
	
	/**
	 * generate final buffer by logical operation of differnt condition	 
	 */
	
	public void getResult() 
	{
		/*String tempbuffer[] ={};
		if(Conditionmap.containsKey(LOGICALOPERATOR))
		{
			while(Conditionmap.containsKey(LOGICALOPERATOR))
			{
				try 
				{
					tempbuffer= processCondition(Conditionmap);
					object = gid.getId(Conditionmap.get(Condition).toString());
					Conditionmap.clear();
					initializeData();
					String buffer[] = processCondition(Conditionmap);
					Class cl = Class.forName("logicalOperation."+CapitalChar.makeFirstCharCapital( Conditionmap.get(LOGICALOPERATOR).toString()));
					Logicaloperation master;
					master = (Logicaloperation)cl.newInstance();
					tempbuffer = master.getResult(tempbuffer,buffer);
				}
				catch (ClassNotFoundException e) 
				{
					e.printStackTrace();
				}
				catch (InstantiationException e) 
				{
					e.printStackTrace();
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
			}
		}	
		else
			tempbuffer=processCondition(Conditionmap);
		this.buffer = tempbuffer;*/
		String condition=" ";
		String alies="";
		if(Querycount!=-99)
			alies="p_"+(Querycount-1) +".";
			
		for(int i=0;i<Conditionvector.size();i++)
		{
			Hashtable<Object, Object> Conditionhash = (Hashtable<Object, Object>) Conditionvector.get(i);
			condition = condition+  "lower("+alies +Conditionhash.get(Condition.CONDITIONFIELD).toString() + ")" + Conditionhash.get(Condition.CONDITIONOPERATOR).toString() + " lower('"+ Conditionhash.get(Condition.CONDITIONVALUE).toString() +"')" + " ";
			if(Conditionhash.containsKey(Condition.LOGICALOPERATOR)  )
				condition = condition + Conditionhash.get(Condition.LOGICALOPERATOR).toString()+" ";
		}

		if(QUERY.length()>0 &&condition.length()>2)
			if(QUERY.indexOf("WHERE")==-1)
				QUERY = QUERY + " WHERE " +condition;
			else
				if(Conditionvector.size()>0)
					QUERY = QUERY.substring(0,QUERY.indexOf("WHERE")+5) + " " + condition + QUERY.substring(QUERY.indexOf("WHERE")+5,QUERY.length());
				else
					QUERY = QUERY.replace("WHERE", "");
		else if(Conditionvector.size()>0 && QUERY.length()==0)
		{
			QUERY="SELECT distinct  FROM "+buffer[0] +" 77"+alies.substring(0,alies.length()-1) + " WHERE "+condition;
		}	
		else
			QUERY="SELECT distinct  FROM "+buffer[0] +" "+alies.substring(0,alies.length()-1) ;
	}
	/**
	 * this method return String  array  
	 * @return buffer: String buffer
	 */
	

	/**
	 * this method check particular condition
	 * @param line is string array 
	 * @param Conditionvalue : String  
	 * @param CONDITIONOPERATOR : String  
	 * @param index_pv :int 
	 * @param index_vt :int
	 * @return string array  
	 * this method check given condition 
	 */
	@SuppressWarnings("unchecked")
	public String[] check_condition(String line[],String Conditionvalue,String CONDITIONOPERATOR,int index_pv,int index_vt)
	{
		StringBuffer result = new StringBuffer();
		for(int i=0;i<line.length;i++)
		{
			String splitline[] = line[i].split("\t");
			String value = splitline[index_pv];
			if(value.equals("0"))
				value = splitline[index_vt];
			else
				value =  gid.getId(value);
			 validation.Validation validation;
			try 
			{
				Class C = Class.forName("validation."+CapitalChar.makeFirstCharCapital((CONDITIONOPERATOR)));
				validation = (validation.Validation )C.newInstance();
				validation.initPool();
				if(validation.validate(Conditionvalue,value))
				{
					result.append(line[i]+"\n");
				}
				
			} 
			catch (ClassNotFoundException e) 
			{
				e.printStackTrace();
			}
			catch (InstantiationException e) 
			{
				e.printStackTrace();
			}
			catch (IllegalAccessException e) 
			{
			
				e.printStackTrace();
			}
		}
		return result.toString().split("\n");
	}
	/**
	 *  find condition,condition value, condition field,conditionvalue,
	 * logical operator,condition operator and put into Conditionmap
	 */
	public void initializeData() 
	{
		
	}

	/**
	 * this function set database object and find master id of conditionfield,conditionvalue,
	 * @param db is database object
	 */
	public void initCondition() 
	{
		gid = new GetIdName();
	}
	/**
	 * set object
	 * @param Object is condition object
	 */
	public void setObject(String object) 
	{	
		this.object = object;
	}
	/**
	 * set buffer 
	 * @param buffer[] :String array 
	 */
	
	public static void main(String[] args) 
	{
		Report_master rate = new Report_master();
		rate.initCondition();
		rate.setObject("7268");
		rate.setQuery(" ");
		Vector<Object>  Condition1 = new Vector<Object>();
		Hashtable<Object, Object> table =new Hashtable<Object, Object>();
		table.put(Condition.CONDITIONFIELD, "type");
		table.put(Condition.CONDITIONVALUE, "item");
		table.put(Condition.CONDITIONOPERATOR, "=");
		Condition1.add(table);
		rate.setConditionMap(Condition1);
		rate.initializeData();
		rate.setQueryCount(-99);
		rate.getResult();
		System.out.print(rate.getQuery());
	/*	String line[] = rate.getbuffer();
		for(int i=0;i<line.length;i++)
		{
			System.out.println(line[i]);
		}*/
	}
	/**
 	 *  find all record for given condition <p> when process is add in report_master then addreport is called</p>
	 */
	public void addReport() 
	{
		getResult();
	}
	/** 
	 * this return id which is present in report_master
	 * @return id is String  
	*/
	public String getId() 
	{
		return id;
	}
	/**
	 * set id which is present in report_master 
	 * @param id
	 */
	public void setid(String id) 
	{
		this.id = id;
	}
	/**
 	 *  find all record for given condition <p> when process is add in report_master then addreport is called</p>
	*/
	public void updateReport() 
	{
		getResult();	
	}
	/**
	 * setConditionMap set condition map 
	 * @param Conditionmap  
	 */
	public void setConditionMap(Vector< Object> Conditionmap) 
	{
		this.Conditionvector = Conditionmap;
	}
	
	public String getQuery() 
	{
		return QUERY;
	}


	public void setbuffer(String buffer[]) 
	{
	 this.buffer = buffer;	
	}


	public void setQueryCount(int QueryCount)
	{
		this.Querycount = QueryCount; 
	}


	public void setQuery(String Query) 
	{
		this.QUERY=Query;
	}

}